package com.icetec.iceintel.framework;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.icetec.iceintel.netrequest.InstrutionSender;
import com.umeng.analytics.MobclickAgent;

import com.icetec.R;
import com.icetec.iceintel.model.UserConfig;

import java.util.List;

/**
 * Created by amo on 2015/3/31.
 * 冰块的所有ACTIVITY继承与基本活动
 */
public class IceBaseActivity extends Activity {

    @Override
    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        //设置主题
        if(AppApplication.userConfig.getDaynightSchema().equals(UserConfig.CONTROL_NIGHT)){
            setTheme(R.style.NightTheme);
        }
        else {
            setTheme(R.style.AppTheme);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (!isAppOnForeground()) {
            Log.i(getClass().getSimpleName(),"程序进入后台");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);

        if (isAppOnForeground()) {

            Log.i(getClass().getSimpleName(),"程序进入前台");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public boolean isAppOnForeground() {
        // Returns a list of application processes that are running on the
        // device

        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }
}
